package com.vhall.component.service.chat.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vhall.component.dao.account.AccountMapper;
import com.vhall.component.dao.chat.CommentRecordMapper;
import com.vhall.component.entity.account.AccountEntity;
import com.vhall.component.entity.chat.entity.CommentRecordEntity;
import com.vhall.component.entity.chat.vo.CommentListReqVO;
import com.vhall.component.entity.chat.vo.CommentListRspVO;
import com.vhall.component.entity.chat.vo.CommentSubmissionVO;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.plugin.common.support.VssToken;
import com.vhall.component.plugin.common.support.VssTokenThreadLocal;
import com.vhall.component.plugin.common.utils.BeanCopyUtil;
import com.vhall.component.service.chat.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author shimingming
 * @since 2023/3/20 14:39
 */
@Slf4j
@Service
public class CommentServiceImpl implements CommentService {

    @Value("${paas.apps.lite.appId}")
    public String appId;

    @Resource
    CommentRecordMapper commentRecordMapper;

    @Resource
    AccountMapper accountMapper;

    @Override
    public void commentSubmission(CommentSubmissionVO reqVo) {


        Preconditions.checkArgument(org.apache.commons.lang3.StringUtils.isNotBlank(reqVo.getTextContent()), "评论内容不能为空！");

        VssToken vssToken = VssTokenThreadLocal.getToken();
        CommentRecordEntity commentRecordEntity = new CommentRecordEntity();

        AccountEntity accountEntity = accountMapper.selectById(vssToken.getThirdPartyUserId());
        if (accountEntity != null) {
            commentRecordEntity.setCreatorNickname(accountEntity.getNickname());
        }

        commentRecordEntity.setAppId(appId);
        commentRecordEntity.setIlId(reqVo.getIlId());
        commentRecordEntity.setRoomId(reqVo.getRoomId());
        commentRecordEntity.setCreatorId(vssToken.getThirdPartyUserId().toString());

        commentRecordEntity.setTextContent(reqVo.getTextContent());
        commentRecordEntity.setCreatedAt(LocalDateTime.now());
        commentRecordMapper.insert(commentRecordEntity);
    }

    @Override
    public PageVO<CommentListRspVO> lists(CommentListReqVO reqVO) {

        Preconditions.checkArgument(reqVO.getIlId() != null, "房间id 不能为空！");

        LambdaQueryWrapper<CommentRecordEntity> where = Wrappers.lambdaQuery();
        where.eq(CommentRecordEntity::getIlId, reqVO.getIlId());
        where.eq(StringUtils.isNotBlank(reqVO.getCreatorNickname()), CommentRecordEntity::getCreatorNickname, reqVO.getCreatorNickname());
        where.likeRight(StringUtils.isNotBlank(reqVO.getTextContent()), CommentRecordEntity::getTextContent, reqVO.getTextContent());

        where.orderByDesc(CommentRecordEntity::getId);
        Page<CommentRecordEntity> page = new Page<>(reqVO.getPage(), reqVO.getPageSize());
        page = commentRecordMapper.selectPage(page, where);
        List<CommentRecordEntity> records = page.getRecords();
        List<CommentListRspVO> commentListRspVos = BeanCopyUtil.batchCopy(records, CommentListRspVO::new);
        return new PageVO<>(reqVO.getPage(), reqVO.getPageSize(), (int) page.getTotal(), commentListRspVos);
    }
}
